package com.android.aqq;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.aqq.bean.ChatState;
import com.android.aqq.bean.QQBuddy;
import com.android.aqq.bean.QQGroup;
import com.android.aqq.bean.ViewHolder;

import edu.tsinghua.lumaqq.qq.QQ;

public class ChatDashboardActivity extends Activity {
	Intent mIntent ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		final Window win = getWindow();
//		// No Statusbar
//		win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.chat_dashboard);
		
		mIntent = getIntent();
		int chatId = mIntent.getIntExtra("CHAT_ID", 0);
		
		Map<Integer, ChatState> map = aQQActivity.mChatStateMap;
		List<ChatState> list = new ArrayList<ChatState>();
		Iterator<Integer> iterator = map.keySet().iterator();
		while(iterator.hasNext()) {
			int id = iterator.next();
			if (chatId != id) {
				ChatState chat = map.get(id);
				if (chat.getUnread()>0) {
					list.add(0, map.get(id));
				} else {
					list.add(map.get(id));
				}
			}
		}
		
		if (list.size()==0) {
			TextView v = (TextView) findViewById(R.id.hint_text);
			v.setVisibility(View.VISIBLE);
		}
		
		ChatListAdapter adapter = new ChatListAdapter(this, list);
		
		int screenWidth;
		WindowManager windowManager = getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		screenWidth = display.getWidth();
		
        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setMinimumWidth(screenWidth);
        g.setAdapter(adapter);
		
        g.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> av, View arg1, int pos,
					long arg3) {
				
				ChatState chat = (ChatState)av.getAdapter().getItem(pos);
				
				setResult(RESULT_OK, (new Intent()).putExtra("CHAT_ID", chat.getId()));
				
				finish();
			}
        });
	}


    public class ChatListAdapter extends BaseAdapter {
    	private Context mContext = null;
    	private List<ChatState> mChatStats = null;

		public List<ChatState> getFriends() {
			return mChatStats;
		}

		public ChatListAdapter(Context context, List<ChatState> chatState) {
        	this.mContext = context;
        	this.mChatStats = chatState; 
        }

		public int getCount() {
			return mChatStats.size();
		}

		public ChatState getItem(int position) {
			ChatState chat = mChatStats.get(position);
 			return chat;
		}

		public long getItemId(int position) {
			return position;
		}
		
		public View getView(int pos, View convertView, ViewGroup vg) {
			final LayoutInflater inflater = LayoutInflater.from(mContext);
        	final ViewHolder holder;
        	if (convertView == null) {
        	    //使用XML文件填充整个布局
        	    convertView = inflater.inflate(R.layout.chat_list, null);
        	    holder = new ViewHolder();//生成存放数据的组件模型
        	    holder.mNick = (TextView) convertView.findViewById(R.id.nick);
        	    holder.mUserHead = (ImageView) convertView.findViewById(R.id.user_head);
        	    convertView.setTag(holder); //将存放数据的组件模型加入到整个View视图中
        	} else {
        	     holder = (ViewHolder) convertView.getTag();
        	}
        	ChatState chat = getItem(pos);
        	if (chat.getType() == 1) {
        		final QQBuddy buddy = chat.getBuddy();
            	byte status = buddy.getStatus();
            	holder.mNick.setText(buddy.toString() + " (" + chat.getUnread() + ")");
            	
            	switch(status) {
            	case QQ.QQ_STATUS_ONLINE:
            		holder.mUserHead.setImageDrawable(getResources().getDrawable(R.drawable.head_online));
            		holder.mNick.setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
            		break;
            	case QQ.QQ_STATUS_QME:
            		holder.mUserHead.setImageDrawable(getResources().getDrawable(R.drawable.head_qme));
        			holder.mNick.setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
        			break;
            	case QQ.QQ_STATUS_OFFLINE:
        			holder.mUserHead.setImageDrawable(getResources().getDrawable(R.drawable.head_offline));
        			holder.mNick.setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
        			break;
            	case QQ.QQ_STATUS_AWAY:
            		holder.mUserHead.setImageDrawable(getResources().getDrawable(R.drawable.head_away));
            		holder.mNick.setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
            		break;
            	case QQ.QQ_STATUS_BUSY:
            		holder.mUserHead.setImageDrawable(getResources().getDrawable(R.drawable.head_busy));
            		holder.mNick.setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
            		break;
            	case QQ.QQ_STATUS_QUIET:
            		holder.mUserHead.setImageDrawable(getResources().getDrawable(R.drawable.head_quiet));
            		holder.mNick.setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
            		break;
            	}
    		} else {
    			final QQGroup group = chat.getQQGroup();
    			holder.mNick.setText(group.getName() + " (" + chat.getUnread() + ")");
    			holder.mUserHead.setImageDrawable(getResources().getDrawable(R.drawable.head_online));
        		holder.mNick.setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
    		}
			return convertView;
		}
    }
}
